Topic 2 Multiprocessing (5)

Leedehai
Monday, April 24, 2017
Friday, April 28, 2017

2.7 How does multiprocessing works?

Q1: How does one process behave as if it is the only process running in the memory and occupies all the virtual memory space?
Q2: The number of processes running on a machine is almost always more than the number of CPU's logical cores. How does the CPU handle them?

2.7.1 Virtual-physical memory mapping

A simplified illustration.

┌───────────────────────────┐ MMU look-up ┌───────────────────────────┐ │ │ ┌──────▶│ 1207: page 3 │ │ kernel space │ │ ├───────────────────────────┤ │ │ │ ┌────▶│ 1207: page 0 │ ├───────────────────────────┤ │ │ ├───────────────────────────┤ │ (unused, unmapped) │ │ │ │ (taken by another process)│ ├───────────────────────────┤ │ │ ├───────────────────────────┤ │ 1207: page 0 │─────│─┘ │ │ ├───────────────────────────┤ │ │ ... │ │ 1207: page 1 │─────│─┐ │ │ ├───────────────────────────┤ │ │ │ │ │ 1207: page 2 │─────│┐│ ├───────────────────────────┤ ├───────────────────────────┤ │└│────▶│ 1207: page 2 │ │ (unused, unmapped) │ │ │ ├───────────────────────────┤ ├───────────────────────────┤ │ └────▶│ 1207: page 1 │ │ ... │ │ ├───────────────────────────┤ │ │ │ │ (taken by another process)│ ├───────────────────────────┤ │ ├───────────────────────────┤ │ 1207: page 3 │─────┘ │ │ ├─────────────────0x6000acff┤ │ ... │ │ 1207: page 4 │───────┐ │ │ ├─────────────────0x6000ac00┤ │ │ │ │ ... │ │ ├─────────────────0x2cccfaff┤ │ │ └────▶│ 1207: page 4 │ └───────────────────────────┘ └─────────────────0x2cccfa00┘ virtual memory of proc. 1207 physical memory

2.7.2 Memory manager and lazy mapping

The memory manager is a component of a OS kernel that is responsible for virtual-physical memory mapping.

2.7.2.1 The layout of an executable

Illustrated below is the general layout of an executable file, the final output of a compiling system.

SIDE NOTE:
There are many executable file format, such as Unix's 32-bit ELF and 64-bit ELF, macOS and iOS's Mach-O, and Window's PE.

┌─────────────────────────────┐ │ bss info │ <- info of uninitialized global variables, │ (uninitialized global var.) │ like their sizes ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │ rodata │ <- read-only global variables, i.e. global │ (const global var.) │ variables declared with "const". ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │ data │ <- other global variables. │ (global var.) │ ├─────────────────────────────┤ │ │ │ │ │ │ <- binary instructions stored here. │ text │ │ (binary instructions) │ │ │ │ │ │ │ └─────────────────────────────┘file start

Note that the "bss" segment in the executable file does not store the uninitialized global variables themselves, only their basic information (e.g. their sizes). That being said, uninitialized global variables of a few primitive types in C/C++, like int and bool, can be filled with 0 during loading time.

2.7.2.2 Memory management

This conservative mapping mechanism is termed lazy mapping, which is meant to save physical memory space and thus support multiprocessing, with the cost of time efficiency.

2.7.3 Scheduler and context switch

The scheduler is a component of the kernel that is responsible for context switching, i.e. interleaving the execution flow of processes.

2.7.3.1 Context switch

process A OS process B ┃ : ┃ #101 (instruction) │ ┃ #102 ▼ time ─ ─ ▼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ tick save A's reg. value to memory load B's reg. value from memory ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┰ ─ ─ ┃ : ┃ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▼ ─ ─ tick save B's reg. value to memory load A's reg. value from memory ─ ─ ┰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┃ #103 ┃ #104 : : ─ ─ ▼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ tick

2.7.3.2 Three classes of processes: the running, ready, and blocked

A process's lifetime illustrated in a state diagram:

┌───────────────┐ ┌────────────┐ │ running │ exit(), SIGKILL... │ terminated │ ┌───┤ (one per core)├────────────────────▶ (zombie) │ => reap │ └────┬─────▲────┘ └────────────┘ │ switch│ │switch │ out│ │in sleep()│ ┌────▼─────┴────┐ waitpid()│ │ ready │ admitted&initiated ┌────────────┐ SIGSTOP│ │ (ready queue) ◀────────────────────┤ created │ <= create ...│ └───────▲───────┘ └────────────┘ │ │ │ │ event of interest │ ┌───────┴───────┐ │ │ blocked │ └───▶ (blocked set) │ └───────────────┘
EOF